package cn.lzit.sy4;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CartServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.print("<P>这里是结账处,您的姓名以及选择的商品是：<br />");
        HttpSession session = request.getSession();
        String personName = (String) session.getAttribute("customerName");
        out.println("<br>您的姓名：" + personName);
        Enumeration<String> enumGoods = session.getAttributeNames();
        out.println("<br>购物车中的商品：<br>");
        while (enumGoods.hasMoreElements()) {
            String key = enumGoods.nextElement();
            String goods = (String) session.getAttribute(key);
            if (!(goods.equals(personName))) {
                out.println(goods + "<br>");
            }
        }

        out.println("<P>点击超链接，链接到food.html的页面，购买食品。");
        out.println("<A href='food.html'>  欢迎去food.html！</A>");
        out.println("<P>点击超链接，链接到login.html的页面,去修改姓名。");
        out.println("<A href='login.html'>  欢迎去login.html！</A>");
        out.println("<P>点击超链接，链接到logout页面,退出");
        out.println("<A href='/chapter05/logout'> 退出！</A>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用doGet方法处理POST请求（这通常不是最佳实践，因为GET和POST请求应该有不同的处理逻辑）
        // 但在这个简单的例子中，我们假设它们可以共享相同的处理逻辑
        doGet(request, response);
    }
}
